package com.qianniu.napi.admin.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qianniu.napi.admin.entity.GoodsParamter;
import com.qianniu.napi.admin.entity.User;
import com.qianniu.napi.admin.service.IGoodsParamterService;
import com.qianniu.napi.common.util.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.Date;

/**
 * <p>
 * 商品参数表
 * </p>
 *
 * @author chenxuebin
 * @since 2018-08-22
 */
@Controller
public class GoodsParamterController {

    private static final Logger logger = LoggerFactory.getLogger(GoodsParamterController.class);

    @Autowired
    private IGoodsParamterService goodsparamterService;

    @RequestMapping("admin/goodsparamter/index")
    public String index() {
        return "admin/goodsparamter";
    }

    @RequestMapping("/admin/goodsparamter/list")
    @ResponseBody
    public Page<GoodsParamter> list(@RequestParam(value = "limit", required = false) int limit,
                                    @RequestParam(value = "offset", required = false) int offset) {
        User user = ObjectUtil.getSessionUser();
        Wrapper<GoodsParamter> wrapper = new EntityWrapper<GoodsParamter>().eq("mid", user.getMid());
        if (user.getMpid() != null) {
            wrapper.eq("mpid", user.getMpid());
        }
        wrapper.orderBy("id", true);
        Page<GoodsParamter> page = goodsparamterService.selectPage(new Page<GoodsParamter>(offset / limit + 1, limit), wrapper);
        return page;
    }

    @RequestMapping("/admin/goodsparamter/add")
    public String add(@Valid GoodsParamter bean, BindingResult bindingResult) {
        // 字段校验
        if (bindingResult.hasErrors()) {
            logger.debug("There are errors! {}", bindingResult);
            return null;
        }
        bean.setCreatetime(new Date());
        bean.setMid(ObjectUtil.getMid());
        bean.setUid(ObjectUtil.getUid());
        bean.insert();
        return "admin/goodsparamter";
    }

    @RequestMapping("/admin/goodsparamter/getGoodsParamterById")
    @ResponseBody
    public GoodsParamter getGoodsParamterById(long id) {
        GoodsParamter goodsparamter = goodsparamterService.selectById(id);
        return goodsparamter;
    }

    @RequestMapping("/admin/goodsparamter/deleteOne")
    @ResponseBody
    public int deleteOne(long id) {
        boolean isSucc = goodsparamterService.deleteById(id);
        return isSucc ? 1 : 0;
    }

    @RequestMapping("/admin/goodsparamter/changeState")
    @ResponseBody
    public String changeState(long id, int state) {
        GoodsParamter bean = new GoodsParamter();
        bean.setId(id);
//        bean.setState(state);
        bean.setModifytime(new Date());
        bean.setModifier(ObjectUtil.getUid());
        goodsparamterService.updateById(bean);
        return "success";
    }

    @RequestMapping("/admin/goodsparamter/modify")
    public String update(GoodsParamter bean) {
        bean.setModifytime(new Date());
//        bean.setModifier(ObjectUtil.getUid());
        boolean isSuccc = goodsparamterService.updateById(bean);
        if (isSuccc) {
            return "redirect:index";
        }
        return "error";
    }

}
